package com.github.codemperor.core.logger.compute;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * mapper以及reducer处理的数据类
 */
public class Context {
    /**
     * 最终结果类
     */
    private Map<Object, Object> result = new HashMap<>();

    public Map<Object, Object> getResult() {
        return result;
    }

    /**
     * 相同key的数据块
     */
    private Map<Object, List<Object>> mapperBlock = new ConcurrentHashMap<>();

    public Map<Object, List<Object>> getMapperBlock() {
        return mapperBlock;
    }

    /**
     * 将数据进行统一key处理
     */
    public void map(Object key, Object value) {
        List<Object> list = mapperBlock.get(key);
        if (list == null) {
            list = new ArrayList<>();
        }
        list.add(value);
        mapperBlock.put(key, list);
    }

    /**
     * 处理最终计算结果
     */
    public void reduce(Object key, Object value) {
        result.put(key, value);
    }
}
